package com.augurit.gzcgwsp.smartformCsdlwj.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.gzcgwsp.smartformCsdlwj.dao.YwHwXmclDao;
import com.augurit.gzcgwsp.smartformCsdlwj.entity.YwHwXmcl;
import com.augurit.gzcgwsp.smartformCsdlwj.service.IYwHwXmclService;
import com.augurit.gzcgwsp.smartformCsdlwj.web.form.YwHwXmclForm;

/*
 * 陆域生活垃圾清扫车辆情况service实现类
 */
@Service
@Transactional
public class YwHwXmclServiceImpl  implements IYwHwXmclService{
	@Autowired
	private YwHwXmclDao ywHwXmclDao;
	
	/**
	 * 获取form
	 */
	public YwHwXmclForm getForm(Long Id) {
		YwHwXmcl entity=ywHwXmclDao.get(Id);
		YwHwXmclForm form=new DozerBeanMapper().map(entity,YwHwXmclForm.class);
		return form;
	}

	public List<YwHwXmclForm> getListByXmId(Long xmid) {
		List<YwHwXmcl> list=this.getListByXmid(xmid);
		List<YwHwXmclForm> forms=new ArrayList<YwHwXmclForm>();
		for(YwHwXmcl entity:list){
			YwHwXmclForm form=new DozerBeanMapper().map(entity,YwHwXmclForm.class);
			forms.add(form);
		}
		return forms;
	}

	/**
	 * 根据xmid查询数据
	 * @param xmid
	 * @return
	 */
	private List<YwHwXmcl> getListByXmid(Long xmid){
		String sql=" from "+YwHwXmcl.class.getName()+" where  xmid="+xmid +" order by cllx";
		List<YwHwXmcl> list=this.ywHwXmclDao.find(sql);
		return list;
	}


	public void SaveOrUpdate(YwHwXmclForm form) {
		YwHwXmcl entity;
		if(form != null && form.getId() != null){
			entity =ywHwXmclDao.get(form.getId());
		}else{
			entity = new YwHwXmcl();
		}
		new DozerBeanMapper().map(form,YwHwXmcl.class);
		this.ywHwXmclDao.save(entity);
	}

	public void SaveOrUpdate(YwHwXmcl entity) {
		this.ywHwXmclDao.save(entity);
	}

	public void parseYwHwXmcl(HSSFSheet hssfsheet,Long xmid){
		try {
			int startrow =0;
			int endrow =0;
			HSSFRow hssfrow = hssfsheet.getRow(0);// 第一行
			for (int j = 1; j < hssfsheet.getPhysicalNumberOfRows(); j++) {
				hssfrow = hssfsheet.getRow(j);
				if(hssfrow != null){
					String value = getCellValue(hssfrow, 0);
					if(value.equals("项目情况登记表（二）")){
						startrow = j;
					}else if(value.equals("项目情况登记表（三）")){
						endrow = j;
					}
				}
			}
			if(startrow>0 && endrow >0){
				for(int i =startrow+2;i<endrow;i++){
					hssfrow = hssfsheet.getRow(i);
					if(hssfrow != null){
						String cllx = getCellValue(hssfrow, 1);
						String clph = getCellValue(hssfrow, 2);
						String clxh = getCellValue(hssfrow, 3);
						String fdjh = getCellValue(hssfrow, 4);
						String clzzl = getCellValue(hssfrow, 5);
						String clzcrq = getCellValue(hssfrow, 6);
						if(cllx!=null && cllx.length()>0 && clph != null && clph.length()>0){
							YwHwXmcl ywHwXmcl = new YwHwXmcl();
							ywHwXmcl.setCllx(cllx);
							ywHwXmcl.setClph(clph);
							ywHwXmcl.setClxh(clxh);
							ywHwXmcl.setClzcrq(clzcrq);
							ywHwXmcl.setClzzl(clzzl);
							ywHwXmcl.setFdjh(fdjh);
							ywHwXmcl.setXmid(xmid);
							this.SaveOrUpdate(ywHwXmcl);
						}
						
					}
				}
			}
						
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String getClinfo(Long xmid){
		String clinfo="";
		List<YwHwXmclForm> list = this.getListByXmId(xmid);
		if(list!=null){
			clinfo +="，";
			for(int i=0; i<list.size();i++){
				YwHwXmclForm clform =list.get(i);
				int sl =1;
				String hpSum ="";
				hpSum += clform.getClph()+";";
				for(int k=i+1;k<list.size();k++){
					YwHwXmclForm clform2 =list.get(k);
					if(clform2.getCllx().equals(clform.getCllx())){
						hpSum += clform2.getClph()+";";
						sl++;
						i++;
					}else break;
				}
				hpSum = hpSum.substring(0, hpSum.length()-2);
				clinfo += clform.getCllx()+sl+"台（"+hpSum+"）;";
			}
		}
		return clinfo;
	}
	
	private String getCellValue(HSSFRow hssfrow,int col){
		String value;
		if (hssfrow.getCell((short) col) == null) {
			value = "";
		} else if (hssfrow.getCell((short) col).getCellType() == 0) {
			value = new Double(hssfrow.getCell((short) col).getNumericCellValue()).toString();
		} else {// 如果EXCEL表格中的数据类型为字符串型
			value = hssfrow.getCell((short) col).getStringCellValue().trim();
		}
		return value;
	}
}
